/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 6/26/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define SubCategoryDao class. This class inherit from  SubCategoryDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class SubCategoryDao : SubCategoryDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public SubCategoryDao() { }
        static SubCategoryDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(SubCategoryDao).FullName);
        }
        public static bool Publish(int subCateID)
        {
            bool result = false;
            String methodName = "Publish";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spPublishSubCategory");

            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "SubCategoryId", DbType.Int32, subCateID);

            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        public static bool Order(int SubCategoryId, int type)
        {
            bool result = false;
            String methodName = "Order";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spOrderSubCategory");

            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "SubCategoryId", DbType.Int32, SubCategoryId);
            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "Type", DbType.Int32, type);


            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        private static SubCategory GetAllSubCategoryByCategoryDataReader(IDataReader reader, string LanguageId)
        {
            String methodName = "GetAllSubCategoryByCategoryDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                SubCategory result = new SubCategory();
                //read SubCategoryId field data.

                result.SubCategoryId = ManagerDao.GetintDataFromDataReader(reader, "SubCategoryId");             
                result.Published = ManagerDao.GetstringDataFromDataReader(reader, "Published");
                
                List<SubCategory_Trans> lstTrans = new List<SubCategory_Trans>();
                SubCategory_Trans objTrans = new SubCategory_Trans();
                objTrans.SubCategoryName = ManagerDao.GetstringDataFromDataReader(reader, "SubCategoryName");
                lstTrans.Add(objTrans);
                result.SubCategory_TransCollection = lstTrans;

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static List<SubCategory> GetAllSubCategoryByCategory(int cateId,string languageId)
        {
            String methodName = "GetAllSubCategoryByCategory";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<SubCategory> listSubCategory = new List<SubCategory>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllSubCategoryByCategory");
                ManagerDao.AddInParameter(dbCommand, "CategoryId", DbType.Int32, cateId);
                ManagerDao.AddInParameter(dbCommand, "LanguageId", DbType.String, languageId);
                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listSubCategory.Add(GetAllSubCategoryByCategoryDataReader(reader,languageId));
                    }
                    reader.Close();
                }

                return listSubCategory;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
    }
}
        #endregion